/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.model;

import java.io.Serializable;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

/**
 * 公式（审核公式，计算公式）
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="Rpt_Formula")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Formula implements Comparable<Object>, Serializable {
	
	private static final long serialVersionUID = 7635555887744864706L;
	
	public static final int TYPE_QIANGZHISHENHE = 1;
	public static final int TYPE_YIBANSHENHE = 2;
	public static final int TYPE_JISUANGONGSHI = 3;
	
	public static final int RUNAT_SERVER = 1;
	public static final int RUNAT_CLIENT = 2;
	public static final int RUNAT_BOTH = 3;
	
	public static final int EXECUTEPOINT_ADD = 1;
	public static final int EXECUTEPOINT_EDIT = 2;
	public static final int EXECUTEPOINT_VIEW = 3;

	@Id
	@Column(name="Formula_GUID",length=38)
	@GenericGenerator(name="Formula-assigned",strategy="assigned")
	private String guid;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="Template_GUID")
	private Template template;
	
	@Column(name="Formula_Name",length=100)
	private String name;
	
	@Column(name="Formula_Description",length=200)
	private String description;
	
	/**
	 * 1: runat=server
	 * 2:runat=client
	 */
	@Column(name="Formula_runat")
	private Integer runat;
	
	/**
	 * 审核类型：
	 *	1：强制审核
	 *	2：一般性审核
	 *	3：计算公式
	 */
	@Column(name="Formula_Type")
	private Integer type;
	
	/**
	 * 启用状态
	 *	0：不启用
	 *	1：启用
	 */
	@Column(name="isEnabled")
	private Boolean enabled;
	
	@Lob
	private String formula;
	
	@Basic
	private Double tabIndex;
	
	/**
	 * 执行点,3>2>1：
	 *	1：add
	 *	2: modify
	 *	3: view
	 */
	@Column(name="Formula_executepoint")
	private Integer executePoint;
	
	public Integer getExecutePoint() {
		return executePoint;
	}

	public void setExecutePoint(Integer executePoint) {
		this.executePoint = executePoint;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getFormula() {
		return formula;
	}

	public void setFormula(String formula) {
		this.formula = formula;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getRunat() {
		return runat;
	}

	public void setRunat(Integer runat) {
		this.runat = runat;
	}

	public Double getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(Double tabIndex) {
		this.tabIndex = tabIndex;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}
	
	@Transient
	public String getAliasNameByJisuangongshi(String name){
		if(formula==null)
			return null;
		
		String lowCaseFormula = formula.toLowerCase();
		name = name.toLowerCase();
		
		int iPos1 = name.indexOf("@");
		if(iPos1<0){
			name = "@" + name;
		}
		
		int iPos2 = lowCaseFormula.indexOf(name);
		if(iPos2<0)
			return null;
		
		int iPos3 = lowCaseFormula.lastIndexOf("=", iPos2);
		if(iPos3<0)
			return null;
		
		int iPos4 = lowCaseFormula.lastIndexOf("&", iPos3);
		if(iPos4<0)
			return null;
		
		String value = lowCaseFormula.substring(iPos4+1, iPos3);
		if(value==null)
			return null;
		return value.trim();
	}

	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		Formula myClass = (Formula) object;
		return new CompareToBuilder().append(this.tabIndex, myClass.tabIndex).append(this.type, myClass.type).append(this.enabled, myClass.enabled).append(this.runat, myClass.runat).append(this.description, myClass.description).append(this.guid, myClass.guid).append(this.formula, myClass.formula).append(this.name, myClass.name).toComparison();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof Formula)) {
			return false;
		}
		Formula rhs = (Formula) object;
		return new EqualsBuilder().appendSuper(super.equals(object)).append(this.tabIndex, rhs.tabIndex).append(this.type, rhs.type).append(this.enabled, rhs.enabled).append(this.runat, rhs.runat).append(this.description, rhs.description).append(this.guid,rhs.guid).append(this.formula, rhs.formula).append(this.name, rhs.name).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(379615865, 1401898537).appendSuper(super.hashCode()).append(this.tabIndex).append(this.type).append(this.enabled).append(this.runat).append(this.description).append(this.guid).append(this.formula).append(this.name).toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("tabindex", this.tabIndex).append("formula", this.formula).append("name", this.name).append("runat", this.runat).append("description", this.description).append("enabled", this.enabled).append("type", this.type).append("guid", this.guid).toString();
	}
}